package cc.laop.utils;

import java.text.ParseException;
import java.util.Date;
import java.util.TimeZone;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;

public class DateUtil {

	public static final String Pattern = "yyyy-MM-dd HH:mm:ss";

	public static String getCurrentTime() {
		return parseDate(new Date(), Pattern);
	}

	public static String parseDate(Date date, String formatStr) {
		TimeZone timeZone = TimeZone.getTimeZone("Asia/Shanghai");
		String time = DateFormatUtils.format(date, formatStr, timeZone);
		return time;
	}

	public static String parseDate(Date date) {
		return parseDate(date, Pattern);
	}

	public static Date parseString(String time) {
		Date date = null;
		try {
			date = DateUtils.parseDate(time, Pattern);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	public static int differ(Date time1, Date time2, int surplus) {
		long differ = differ(time1, time2);
		long days = differ / (1000 * 60 * 60 * 24);
		return (int) (surplus - days);
	}

	public static long differ(Date time1, Date time2) {
		long t1 = time1.getTime();
		long t2 = time2.getTime();
		return t1 - t2;
	}

	public static int differDays(Date time1, Date time2) {
		long differ = differ(time1, time2);
		return (int) (differ / (1000 * 60 * 60 * 24));
	}

	public static String formatDuration(long duration) {
		long days = duration / (1000 * 60 * 60 * 24);
		long hours = (duration % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
		long minutes = (duration % (1000 * 60 * 60)) / (1000 * 60);
		// long seconds = (duration % (1000 * 60)) / 1000;
		String time = "";
		if (days != 0) {
			time += days + "天";
		}
		if (hours != 0) {
			time += hours + "小时";
		}
		if (minutes != 0) {
			time += minutes + "分钟";
		}
		return time;
	}

	public static void main(String[] args) {
		long differ = 123678900;
		System.out.println(formatDuration(differ));
	}
}
